package txt;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

public class TextDownload {

    public static File resource = FileUtil.touch("E:\\IDEAProjects\\lemon\\lemon-mail\\mail-smtp\\src\\main\\resources");


    public static void main(String[] args) {

        getText();
    }

    public static void getMulu() {
        ArrayList<String> mulu = new ArrayList<>();
        String baseUrl = "https://m.cz-boiler.com/yd/94961166809/?page={}";
        for (int i = 1; i < 10; i++) {
            String url = StrUtil.format(baseUrl, i);
            String body = HttpUtil.get(url);
            Document doc = Jsoup.parse(body);
            List<Element> elements = doc.select("div.block-box:nth-child(10) > ul:nth-child(1) > li");
            for (Element element : elements) {
                Elements selected = element.select("a");
                String text = selected.text();
                String href = selected.attr("href");
                System.out.println(text + " : https://m.cz-boiler.com" + href);
                mulu.add(text);
                mulu.add("https://m.cz-boiler.com" + href);
            }
            FileUtil.appendUtf8Lines(mulu, resource + "\\mulu.txt");
        }


    }


    public static void getText() {
        int count = 0;
        ArrayList<String> txts = new ArrayList<>();
        List<String> mulu = FileUtil.readUtf8Lines(resource + "\\mulu.txt");
        for (String url : mulu) {

            if (url.contains("http")) {
                txts.add(url + "\n");
                for (int i = 1; i <= 3; i++) {
                    String real_url = url;
                    if (i > 1) {
                        real_url = url.replace(".html", "_" + i + ".html");
                    }
                    String body = HttpUtil.get(real_url);
                    // 解析 HTML 后获取文档对象
                    Document doc = Jsoup.parse(body);
                    String txt = doc.select("#txt").first().text();
                    txt = NovelSegmenter.segment(txt);
                    System.out.println(txts.get(0) + " : " + real_url + " : " + txt);
                    txts.add(txt + "\n");
                }
            } else {
                txts.add("\n" + url);
            }
            FileUtil.appendUtf8Lines(txts, resource + "\\text.txt");
            txts.clear();
            count++;
            System.out.println("已下载：" + count);
        }


    }


}

class NovelSegmenter {
    // 预编译正则表达式（提升性能）
    private static final Pattern BASIC_SPLIT = Pattern.compile("(?<![“‘])([。！？])(?!”)");
    private static final Pattern DIALOG_SPLIT = Pattern.compile("([^\\n])(“[^”]+”)");
    private static final Pattern SCENE_SPLIT = Pattern.compile("(?<!\\n)(第二天|凌晨|黄昏|第[0-9一二三四五六七八九十]+[章节])");
    private static final Pattern EMPTY_LINE_CLEAN = Pattern.compile("\\n{3,}");

    // 核心分段方法
    public static String segment(String text) {
        return EMPTY_LINE_CLEAN.matcher(
                SCENE_SPLIT.matcher(
                        DIALOG_SPLIT.matcher(
                                BASIC_SPLIT.matcher(text).replaceAll("$1\n")  // 基础标点分段
                        ).replaceAll("$1\n\n$2\n")  // 对话前后空行
                ).replaceAll("\n$1")  // 场景/章节前换行
        ).replaceAll("\n\n");  // 清理多余空行
    }

    // 测试示例
    // public static void main(String[] args) {
    //     String novel = "第一章序章他推开木门。吱呀声打破寂静！“有人吗？”她问。他指向里屋。第二天雨停了。";
    //     System.out.println(segment(novel));
    // }
}